package com.ledim.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.ledim.activity.base.LeDimBaseActivity;
import com.ledim.adapter.GoldDetailAdapter;
import com.ledim.bean.CoinDetailBean;
import com.ledim.bean.base.BaseListResponse;
import com.ledim.service.ApiService;
import com.ledim.service.LedimApiServiceFactory;
import com.ledim.service.LedimHttpObserver;
import com.ledim.utils.LogUtil;
import com.ledim.utils.ToastUtil;
import com.letv.android.young.client.R;
import java.util.List;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 我的金币页面
 */
public class MyGoldActivity extends LeDimBaseActivity {

    private ListView goldDetailListView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();

        ImageView mBack = findViewByIdExt(R.id.iv_back);
        mBack.setVisibility(View.VISIBLE);
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        ((TextView) findViewById(R.id.tv_title)).setText(R.string.my_gold);

        findViewByIdExt(R.id.bt_gold_supply).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyGoldActivity.this, GoldCheatsActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
            }
        });

        ((TextView) findViewById(R.id.tv_mygold_des)).setText(getIntent().getExtras()
                                                                         .getString("mGoldCountDes"));
        String mGoldCount = getIntent().getExtras().getString("mGoldCount");
        if (mGoldCount != null) {
            SpannableString spannableStr = new SpannableString(mGoldCount + " 发");
            //设置子图大小
            spannableStr.setSpan(new AbsoluteSizeSpan(48),
                    mGoldCount.length() + 1,
                    mGoldCount.length() + 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            ((TextView) findViewById(R.id.tv_mygold_count)).setText(spannableStr);
        }
        getCoinsDetail();

    }

    private void initView() {
        goldDetailListView = findViewByIdExt(R.id.lv_gold_detail);
    }

    /**
     * 获取金币详情
     */
    public void getCoinsDetail() {

        LedimApiServiceFactory.createService(ApiService.class)
                              .getCoinDetail()
                              .observeOn(AndroidSchedulers.mainThread())
                              .subscribeOn(Schedulers.io())
                              .subscribe(new LedimHttpObserver<BaseListResponse<CoinDetailBean>>() {

                                 @Override
                                 public void onCompleted() {
                                     super.onCompleted();
                                 }

                                 @Override
                                 public void onNext(
                                         BaseListResponse<CoinDetailBean> coinDetailResponse) {
                                     super.onNext(coinDetailResponse);

                                     if (coinDetailResponse.success) {
                                         //成功
                                         List<CoinDetailBean> coinDetailList =
                                                 coinDetailResponse.data;
                                         if (coinDetailList.size() > 0 && coinDetailList != null) {
                                             goldDetailListView.setAdapter(new GoldDetailAdapter(MyGoldActivity.this, coinDetailList));
                                         }
                                     } else {
                                         //失败
                                         ToastUtil.toast(coinDetailResponse.message);
                                     }
                                 }

                                 @Override
                                 public void onError(Throwable e) {
                                     super.onError(e);
                                     LogUtil.e(e.getMessage());
                                 }
                             });

    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_mygold;
    }
}
